﻿//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.BusinessLogic.Services;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class TopicControl : BaseNopFrontendUserControl
    {
        #region Private variables

        private string _cssClass;
        private string _topicName;
        private bool _overrideSeo;
        private string _enteredPassword;
        private LocalizedTopic localizedTopic = null;

        private ITopicService _topicService;

        #endregion

        public TopicControl() {
            _topicService = IoC.Resolve<ITopicService>();        
        }

        #region Page Events

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            Load += new EventHandler(TopicControl_Load);
            PreRender += new EventHandler(TopicControl_PreRender);

            btnPassword.Click += new EventHandler(btnPassword_Click);
        }

        void TopicControl_Load(object sender, EventArgs e) {
            pnlError.Visible = false;
            this.BindData();

            if (this.LocalizedTopic != null) {
                if (this.OverrideSEO) {
                    //title
                    string title = this.LocalizedTopic.MetaTitle;
                    if (String.IsNullOrEmpty(title))
                        title = this.LocalizedTopic.Title;
                    if (!string.IsNullOrEmpty(title))
                        SEOHelper.RenderTitle(this.Page, title, true);

                    //meta
                    if (!String.IsNullOrEmpty(this.LocalizedTopic.MetaDescription))
                        SEOHelper.RenderMetaTag(this.Page, "description", this.LocalizedTopic.MetaDescription, true);
                    if (!String.IsNullOrEmpty(this.LocalizedTopic.MetaKeywords))
                        SEOHelper.RenderMetaTag(this.Page, "keywords", this.LocalizedTopic.MetaKeywords, true);
                }
            }
        }

        void TopicControl_PreRender(object sender, EventArgs e) {
            //default button
            this.txtPassword.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnPassword.ClientID + "').click();return false;}} else {return true}; ");
        }

        #endregion

        #region Events

        protected void btnPassword_Click(object sender, EventArgs e) {
            this.EnteredPassword = txtPassword.Text;
            bool passwordOK = ValidatePassword();
            if (!passwordOK) {
                pnlError.Visible = true;
                lError.Text = GetLocaleResourceString("TopicPage.WrongPassword");
            }
            BindData();
        }

        #endregion

        #region Methods

        private void BindData() {
            if (this.LocalizedTopic != null) {
                bool passwordOK = ValidatePassword();
                if (passwordOK) {
                    phPassword.Visible = false;
                    lTitle.Visible = true;
                    lBody.Visible = true;

                    if (!string.IsNullOrEmpty(this.LocalizedTopic.Title)) {
                        lTitle.Text = Server.HtmlEncode(this.LocalizedTopic.Title);
                    }
                    else {
                        lTitle.Visible = false;
                    }
                    if (!string.IsNullOrEmpty(this.LocalizedTopic.Body)) {
                        lBody.Text = this.LocalizedTopic.Body;
                    }
                    else {
                        lBody.Visible = false;
                    }
                }
                else {
                    phPassword.Visible = true;
                    lTitle.Visible = false;
                    lBody.Visible = false;
                }
            }
            else {
                this.Visible = false;
            }
        }

        private bool ValidatePassword() {
            bool passwordOK = true;
            if (this.Topic != null) {
                if (this.Topic.IsPasswordProtected &&
                    !this.Topic.Password.Equals(this.EnteredPassword)) {
                    passwordOK = false;
                }
            }
            return passwordOK;
        }

        #endregion

        #region Properties

        public LocalizedTopic LocalizedTopic {
            get {
                if (localizedTopic == null) {
                    localizedTopic = _topicService.GetLocalizedTopic(this.TopicName, NopContext.Current.WorkingLanguage.LanguageId);
                }
                return localizedTopic;
            }
        }

        public Topic Topic {
            get {
                if (this.LocalizedTopic != null)
                    return this.LocalizedTopic.Topic;
                else
                    return null;
            }
        }

        public string TopicName {
            get { return _topicName; }
            set { _topicName = value; }
        }

        [DefaultValue(true)]
        public bool OverrideSEO {
            get { return _overrideSeo; }
            set { _overrideSeo = value; }
        }

        public string EnteredPassword {
            get { return _enteredPassword; }
            set { _enteredPassword = value; }
        }

        public string CssClass {
            get {
                if (string.IsNullOrWhiteSpace(_cssClass))
                    return string.Empty;
                return " " + _cssClass;
            }
            set { _cssClass = value; }
        }

        public bool ShowHeader {
            get { return topicHeader.Visible; }
            set { topicHeader.Visible = value; }
        }

        #endregion
    }
}
